{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "import torch\n",
    "import torchvision\n",
    "from torch import nn\n",
    "import torch.nn.functional as F\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "IMG_PATH = \"./raw_pic/num_singe.jpg\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用opencv展示图片\n",
    "def img_show_cv(img_name, img):\n",
    "    cv2.imshow(img_name, img)\n",
    "    cv2.waitKey(0)\n",
    "    cv2.destroyAllWindows()\n",
    "\n",
    "# 使用matplotlib展示图片\n",
    "def img_show_plt(img):\n",
    "    plt.imshow(img, cmap=\"gray\")\n",
    "    plt.show()\n",
    "\n",
    "# 导入模型\n",
    "class CNN(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(CNN,self).__init__()\n",
    "        self.conv1 = nn.Conv2d(1,32,kernel_size=3,stride=1,padding=1)\n",
    "        self.pool = nn.MaxPool2d(2,2)\n",
    "        self.conv2 = nn.Conv2d(32,64,kernel_size=3,stride=1,padding=1)\n",
    "        # 两个池化，所以是7*7而不是14*14\n",
    "        self.fc1 = nn.Linear(64*7*7,1024)\n",
    "        self.fc2 = nn.Linear(1024,512)\n",
    "        self.fc3 = nn.Linear(512,10)\n",
    "\n",
    "    def forward(self,x):\n",
    "        x = self.pool(F.relu(self.conv1(x)))\n",
    "        x = self.pool(F.relu(self.conv2(x)))\n",
    "        # 将数据平整为一维的\n",
    "        x = x.view(-1, 64 * 7* 7)\n",
    "        x = F.relu(self.fc1(x))\n",
    "        x = F.relu(self.fc2(x))\n",
    "        x = self.fc3(x)\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(28, 28, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaAAAAGdCAYAAABU0qcqAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAeVUlEQVR4nO3de3BU9fnH8c8mJAtoshhjbhIwgEIViC1KmkERS4aQVkcuf+CtA47FkQZboFYnrYqX30wUW7U6VP9pSZ0RtXS4qDOloyChtgELyjBUmyFMarCQUOmwGwIkIXt+fzBuuxIu3+NunmR5v2bOTHb3PDlPTk72k5M9eTbgeZ4nAAD6WJp1AwCACxMBBAAwQQABAEwQQAAAEwQQAMAEAQQAMEEAAQBMEEAAABODrBv4qmg0qgMHDigrK0uBQMC6HQCAI8/z1N7erqKiIqWlnfk8p98F0IEDB1RcXGzdBgDga9q/f7+GDx9+xsf7XQBlZWVJOtV4dna2cTcAAFeRSETFxcWx5/MzSVoArVy5Us8++6xaW1tVWlqql156SZMnTz5n3Zd/dsvOziaAAGAAO9fLKEm5COHNN9/UsmXLtHz5cn300UcqLS1VZWWlDh06lIzNAQAGoKQE0HPPPaeFCxfqnnvu0dVXX61XXnlFQ4cO1W9/+9tkbA4AMAAlPIC6urq0c+dOVVRU/HcjaWmqqKhQQ0PDaet3dnYqEonELQCA1JfwAPriiy/U09Oj/Pz8uPvz8/PV2tp62vq1tbUKhUKxhSvgAODCYP6PqDU1NQqHw7Fl//791i0BAPpAwq+Cy83NVXp6utra2uLub2trU0FBwWnrB4NBBYPBRLcBAOjnEn4GlJmZqUmTJmnTpk2x+6LRqDZt2qTy8vJEbw4AMEAl5f+Ali1bpvnz5+u6667T5MmT9cILL6ijo0P33HNPMjYHABiAkhJA8+bN07///W899thjam1t1bXXXquNGzeedmECAODCFfA8z7Nu4n9FIhGFQiGFw2EmIQDAAHS+z+PmV8EBAC5MBBAAwAQBBAAwQQ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Vl5cnclMAgAHO+Sq4o0ePqqmpKXa7ublZu3btUk5OjkaMGKElS5bo//7v/3TllVeqpKREjz76qIqKijRr1qxE9g0AGOCcA2jHjh26+eabY7eXLVsmSZo/f77q6ur00EMPqaOjQ/fdd5+OHDmiG264QRs3btTgwYMT1zUAYMBjGClSkp8BoX3JT3+LFy92rqmrq3OukaSenh7nGj+/ZP7tb39zrhk7dqxzjd8BoX6eHv3UpKWZXw+WUAwjBQD0awQQAMAEAQQAMEEAAQBMEEAAABMEEADABAEEADBBAAEATBBAAAATBBAAwAQBBAAwQQABAEwQQAAAE85vxwD0NT+TmdPT031ty8+Uaj/Tj7u7u51rampqnGteffVV5xpJ6urqcq6ZPHmyc82IESOca/zwcwxJ/qZop9pk62RiTwEATBBAAAATBBAAwAQBBAAwQQABAEwQQAAAEwQQAMAEAQQAMEEAAQBMEEAAABMEEADABAEEADDBMFL0e34Gi/odPulnW301jPTaa691rjlx4oRzjSQ99NBDzjXPP/+8c833v/9955o1a9Y41/gdTuuHn0GumZmZSeik/+MMCABgggACAJgggAAAJgggAIAJAggAYIIAAgCYIIAAACYIIACACQIIAGCCAAIAmCCAAAAmCCAAgAmGkaLf8zPs0+/wST+DJP30N3LkSOeajo4O55o9e/Y410jSmDFjnGva2tqca1avXu1c8+mnnzrXXH311c41fgUCgT7b1kDHGRAAwAQBBAAwQQABAEwQQAAAEwQQAMAEAQQAMEEAAQBMEEAAABMEEADABAEEADBBAAEATBBAAAATAc/PJMUkikQiCoVCCofDys7Otm4HF5hoNOpcU1pa6lzz97//3bnmrbfecq753ve+51wj+dsP3d3dzjWFhYXONX6Gfe7fv9+5RpIyMzOdazIyMnxtK5Wc7/M4Z0AAABMEEADAhHMAbd26VbfeequKiooUCAS0fv36uMcXLFigQCAQt8ycOTNR/QIAUoRzAHV0dKi0tFQrV6484zozZ87UwYMHY8vrr7/+tZoEAKQe53dEraqqUlVV1VnXCQaDKigo8N0UACD1JeU1oC1btigvL09jx47VokWLdPjw4TOu29nZqUgkErcAAFJfwgNo5syZevXVV7Vp0yY988wzqq+vV1VVlXp6enpdv7a2VqFQKLYUFxcnuiUAQD/k/Ce4c7n99ttjH0+YMEETJ07U6NGjtWXLFk2fPv209WtqarRs2bLY7UgkQggBwAUg6Zdhjxo1Srm5uWpqaur18WAwqOzs7LgFAJD6kh5An3/+uQ4fPuzrP54BAKnL+U9wR48ejTubaW5u1q5du5STk6OcnBw98cQTmjt3rgoKCrRv3z499NBDGjNmjCorKxPaOABgYHMOoB07dujmm2+O3f7y9Zv58+fr5Zdf1u7du/W73/1OR44cUVFRkWbMmKGnnnpKwWAwcV0DAAY85wCaNm2azja/9E9/+tPXagh9ry/n0Z7pasizGTTI/VoZP8M0JfV6ocy5+Bks+stf/tK55pZbbnGu8bsf0tLc/zo/ePBg55qbbrrJuebtt992rjl27JhzjSQNGTLEucbPz5OfAaupgFlwAAATBBAAwAQBBAAwQQABAEwQQAAAEwQQAMAEAQQAMEEAAQBMEEAAABMEEADABAEEADBBAAEATBBAAAATCX9Lbgw8fTkx+eTJk8416enpzjV33323c40kffjhh841//uW8udr6dKlzjXd3d3ONX4miUv+pjP76W/x4sXONW+99ZZzzdatW51rJGnWrFnONX5+Li5U7CkAgAkCCABgggACAJgggAAAJgggAIAJAggAYIIAAgCYIIAAACYIIACACQIIAGCCAAIAmCCAAAAmGEYKX0MkJX+DLv3UVFZWOtfU19c710jSokWLnGt+8YtfONf4GQDrZyhrX/LzvS0vL3eu8TzPuaahocG5RpLmzJnjXNPT0+Nc09+/t8nCGRAAwAQBBAAwQQABAEwQQAAAEwQQAMAEAQQAMEEAAQBMEEAAABMEEADABAEEADBBAAEATBBAAAATDCOF70GIgUDAueaZZ55xrnnvvfeca37+858710jSo48+6lzjZzhmWpr7737Hjx93rvHzPZKkYDDoXHPy5EnnmsGDBzvX+PHFF1/4qvPzNV2og0X94AwIAGCCAAIAmCCAAAAmCCAAgAkCCABgggACAJgggAAAJgggAIAJAggAYIIAAgCYIIAAACYIIACACYaRQh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3u49+OCDXkNDg9fc3Oy999573re+9S3vyiuv9E6cOGHdesIsWrTIC4VC3pYtW7yDBw/GlmPHjsXWuf/++70RI0Z4mzdv9nbs2OGVl5d75eXlhl0n3rn2Q1NTk/fkk096O3bs8Jqbm70NGzZ4o0aN8qZOnWrcebwBEUCe53kvvfSSN2LECC8zM9ObPHmyt23bNuuW+ty8efO8wsJCLzMz07v88su9efPmeU1NTdZtJd3777/vSTptmT9/vud5py7FfvTRR738/HwvGAx606dP9xobG22bToKz7Ydjx455M2bM8C677DIvIyPDGzlypLdw4cKU+yWtt69fkrdq1arYOsePH/d++MMfepdccok3dOhQb/bs2d7Bgwftmk6Cc+2HlpYWb+rUqV5OTo4XDAa9MWPGeD/96U+9cDhs2/hX8HYMAAAT/f41IABAaiKAAAAmCCAAgAkCCABgggACAJgggAAAJgggAIAJAggAYIIAAgCYIIAAACYIIACACQIIAGDi/wGb+ix7nnpupQAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 读取原图像\n",
    "img = cv2.imread(IMG_PATH)\n",
    "img = cv2.resize(img, (28, 28))\n",
    "# (28, 28, 3) 宽 高 通道数\n",
    "print(img.shape)\n",
    "img_show_plt(img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaAAAAGdCAYAAABU0qcqAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8fJSN1AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAZJElEQVR4nO3df2hV9/3H8dfV6q22yc1iTG7uvLpoW91qzZjTLNi6FoNJBuKvgf0x0CKKLpZp2rU4Wq3bIJsFKS1+61/TFap2QlUqTNDYRLpFh1YR2RpMls2ISWyF3BtjvYr5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U+WN8FcS92797tvF6v27Fjh/vnP//pVq5c6fLy8lxnZ6f1aCn18ssvu4aGBtfW1ub+9re/uYqKCldQUOAuXbpkPVpS9fT0uFOnTrlTp045SW7Lli3u1KlT7r///a9zzrk//OEPLi8vz+3fv9+dOXPGzZ8/35WUlLivvvrKePLE+rbj0NPT41555RXX1NTk2tra3OHDh92PfvQj9+ijj7pr165Zj54wq1evdj6fzzU0NLiOjo7odvXq1eg+q1atcuPHj3dHjhxxJ06ccOXl5a68vNxw6sS723FoaWlxv/3tb92JEydcW1ub279/v5s4caKbPXu28eSxMiJAzjn37rvvuvHjx7uRI0e6mTNnumPHjlmPlHJLlixxxcXFbuTIke673/2uW7JkiWtpabEeK+k++eQTJ+mObenSpc65W7div/HGG66oqMh5vV43Z84c19zcbDt0Enzbcbh69aqbO3euGzt2rBsxYoSbMGGCW7FiRdb9n7T+fv+S3Pbt26P7fPXVV+6Xv/yl+853vuNGjx7tFi5c6Do6OuyGToK7HYfz58+72bNnu/z8fOf1et0jjzzifv3rX7tQKGQ7+G345xgAACbS/jMgAEB2IkAAABMECABgggABAEwQIACACQIEADBBgAAAJggQAMAEAQIAmCBAAAATBAgAYIIAAQBM/D/M/5e2gHBnzgAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 二值化\n",
    "_, img_bin = cv2.threshold(img, 125, 255, cv2.THRESH_BINARY)\n",
    "# 转灰度\n",
    "img_bin = cv2.cvtColor(img_bin, cv2.COLOR_BGR2GRAY)\n",
    "img_show_plt(img_bin)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(28, 28)\n",
      "(1, 28, 28)\n",
      "[8]\n"
     ]
    }
   ],
   "source": [
    "# 将图像从Opencv的Mat转换为Tensor格式\n",
    "# 转narray\n",
    "img_bin = np.array(img_bin).astype(np.float32)\n",
    "print(img_bin.shape)\n",
    "# 扩充维度\n",
    "img_bin = np.expand_dims(img_bin, axis=0)\n",
    "print(img_bin.shape)\n",
    "# 转Tensor\n",
    "img_tensor = torch.from_numpy(img_bin)\n",
    "\n",
    "# 加载模型并进行预测\n",
    "model = CNN()\n",
    "model.load_state_dict(torch.load(\"./mnist_net.pth\")) # 加载State模型\n",
    "model.to('cuda') # 加载数据到GPU\n",
    "model.eval()\n",
    "with torch.no_grad():\n",
    "    i = img_tensor.to(\"cuda\")\n",
    "    _, preds = torch.max(model(i), dim=1)\n",
    "    print(preds.cpu().numpy())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
